import { AppDataSource } from "./data-source"
import { Article } from "./entity/Article";
import { Tag } from "./entity/Tag";


AppDataSource.initialize().then(async () => {
    const a1 = new Article();
    a1.title = 'book1';
    a1.content = 'book111';

    const a2 = new Article();
    a2.title = 'book2';
    a2.content = 'book2222';

    const t1 = new Tag();
    t1.name = 'tag1';

    const t2 = new Tag();
    t2.name = 'tag2';

    const t3 = new Tag();
    t3.name = 'tag3';

    a1.tags = [t1,t2];
    a2.tags = [t1,t2,t3];

    const entityManager = AppDataSource.manager;

    // await entityManager.save(t1);
    // await entityManager.save(t2);
    // await entityManager.save(t3);

    // await entityManager.save(a1);
    // await entityManager.save(a2);

    const books = await entityManager.find(Article, {
        relations:['tags']
    })
    console.log(books);
    console.log(books.map(item => item.tags));
    
    



}).catch(error => console.log(error))
